//
//  GMFColorView.m
//  DrawingDemo
//
//  Created by lijunying on 1/12/2016.
//  Copyright © 2016 jesse. All rights reserved.
//



#import "GMFColorView.h"
#import "GMFColorButton.h"

@interface GMFColorView()

@property (nonatomic, weak) GMFColorButton *btnColor00;
@property (nonatomic, weak) GMFColorButton *btnColor01;
@property (nonatomic, weak) GMFColorButton *btnColor02;
@property (nonatomic, weak) GMFColorButton *btnColor03;
@property (nonatomic, weak) GMFColorButton *btnColor04;
@property (nonatomic, weak) GMFColorButton *btnColor05;
@property (nonatomic, weak) GMFColorButton *btnColor06;
@property (nonatomic, weak) GMFColorButton *btnColor07;

@property (nonatomic, assign) int cintButtonCount;

@property (nonatomic, weak) UIView *viewSeperateLine;

@property (nonatomic, strong) GMFColorButton *btnCurrent;

@end


@implementation GMFColorView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = GMKColor(51, 51, 51);
        self.alpha = 0;
        
        GMFColorButton *btnColor00 = [[GMFColorButton alloc] init];
        [btnColor00 setRadius:intNormalCircleRadius andColor:kColorBlack];
        btnColor00.tag = 100;
        [btnColor00 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnColor00];
        self.btnColor00 = btnColor00;
        _cintButtonCount++;
        
        GMFColorButton *btnColor01 = [[GMFColorButton alloc] init];
        [btnColor01 setRadius:intNormalCircleRadius andColor:kColorRed];
        btnColor01.tag = 101;
        [btnColor01 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnColor01];
        self.btnColor01 = btnColor01;
        _cintButtonCount++;
        
        GMFColorButton *btnColor02 = [[GMFColorButton alloc] init];
        [btnColor02 setRadius:intNormalCircleRadius andColor:kColorOrange];
        btnColor02.tag = 102;
        [btnColor02 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnColor02];
        self.btnColor02 = btnColor02;
        _cintButtonCount++;
        
        GMFColorButton *btnColor03 = [[GMFColorButton alloc] init];
        [btnColor03 setRadius:intNormalCircleRadius andColor:kColorYellow];
        btnColor03.tag = 103;
        [btnColor03 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnColor03];
        self.btnColor03 = btnColor03;
        _cintButtonCount++;
        
        GMFColorButton *btnColor04 = [[GMFColorButton alloc] init];
        [btnColor04 setRadius:intNormalCircleRadius andColor:kColorGreen];
        btnColor04.tag = 104;
        [btnColor04 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnColor04];
        self.btnColor04 = btnColor04;
        _cintButtonCount++;
        
        GMFColorButton *btnColor05 = [[GMFColorButton alloc] init];
        [btnColor05 setRadius:intLargerCircleRadius andColor:kColorDefaultBlue];
        btnColor05.tag = 105;
//        btnColor05.backgroundColor = [UIColor colorWithRed:176/255.0 green:223/255.0 blue:229/255.0 alpha:1.0];
        [btnColor05 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnColor05];
        self.btnColor05 = btnColor05;
        _cintButtonCount++;
        
        GMFColorButton *btnColor06 = [[GMFColorButton alloc] init];
        [btnColor06 setRadius:intNormalCircleRadius andColor:kColorIndigo];
        btnColor06.tag = 106;
        [btnColor06 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnColor06];
        self.btnColor06 = btnColor06;
        _cintButtonCount++;
        
        GMFColorButton *btnColor07 = [[GMFColorButton alloc] init];
        [btnColor07 setRadius:intNormalCircleRadius andColor:kColorPurple];
        btnColor07.tag = 107;
        [btnColor07 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnColor07];
        self.btnColor07 = btnColor07;
        _cintButtonCount++;
        
        _btnCurrent = [[GMFColorButton alloc] init];
        _btnCurrent = _btnColor05;
        
//        UIView *viewSeperateLine = [[UIView alloc] init];
//        viewSeperateLine.backgroundColor = GMKColor(210, 210, 210);
//        [self addSubview:viewSeperateLine];
//        self.viewSeperateLine = viewSeperateLine;
    }
    
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat fltPadding = 5;
    CGFloat fltPaddingBetweenButtons = (screenW - fltPadding * 2 - fltButtonWidth * _cintButtonCount) / (_cintButtonCount - 1);
    
    [_btnColor00 setFrame:CGRectMake(fltButtonWidth * 0 + fltPadding, 0, fltButtonWidth, 50)];
    [_btnColor01 setFrame:CGRectMake(fltButtonWidth * 1 + fltPadding + fltPaddingBetweenButtons * 1, 0, fltButtonWidth, 50)];
    [_btnColor02 setFrame:CGRectMake(fltButtonWidth * 2 + fltPadding + fltPaddingBetweenButtons * 2, 0, fltButtonWidth, 50)];
    [_btnColor03 setFrame:CGRectMake(fltButtonWidth * 3 + fltPadding + fltPaddingBetweenButtons * 3, 0, fltButtonWidth, 50)];
    [_btnColor04 setFrame:CGRectMake(fltButtonWidth * 4 + fltPadding + fltPaddingBetweenButtons * 4, 0, fltButtonWidth, 50)];
    [_btnColor05 setFrame:CGRectMake(fltButtonWidth * 5 + fltPadding + fltPaddingBetweenButtons * 5, 0, fltButtonWidth, 50)];
    [_btnColor06 setFrame:CGRectMake(fltButtonWidth * 6 + fltPadding + fltPaddingBetweenButtons * 6, 0, fltButtonWidth, 50)];
    [_btnColor07 setFrame:CGRectMake(fltButtonWidth * 7 + fltPadding + fltPaddingBetweenButtons * 7, 0, fltButtonWidth, 50)];
    
//    [_viewSeperateLine   setFrame:CGRectMake(0, 0, screenW, 1)];
}


- (void)buttonClick:(GMFColorButton *)button
{
    if (_btnCurrent.tag != button.tag)
    {
        [_btnCurrent selectOrUnselectTheButton:NO];
        _btnCurrent = button;
        [_btnCurrent selectOrUnselectTheButton:YES];
    }

    
    if ([self.delegate respondsToSelector:@selector(colorView: andColor:)])
    {
        UIColor *color;
        
        switch (button.tag - 100)
        {
            case 0:
                color = kColorBlack;
                break;
                
            case 1:
                color = kColorRed;
                break;
                
            case 2:
                color = kColorOrange;
                break;
                
            case 3:
                color = kColorYellow;
                break;
                
            case 4:
                color = kColorGreen;
                break;
                
            case 5:
                color = kColorDefaultBlue;
                break;
                
            case 6:
                color = kColorIndigo;
                break;
                
            case 7:
                color = kColorPurple;
                break;
                
            default:
                break;
        }
        
        [self.delegate colorView:self andColor:color];
    }
}


@end

